package com.zxx.pattern.mediatorPattern.mediator3;

/**
 * 具体中介者（包含了各个对象）
 *
 * @author zxx on 2021/5/11
 */
public class MediatorMainBoard extends Mediator3 {
    private ColleagueCDDevice cdDevice; //光驱
    private ColleagueCPU cpu;           //CPU
    private ColleagueGraphics graphics; //显卡
    private ColleagueSoundCard soundCard;//声卡

    @Override
    protected void changed(Colleague colleague) {
        //如果光驱读取了数据
        if (colleague == cdDevice) {
            handleCD((ColleagueCDDevice) colleague);
        }
        //如果CPU读取
        else if (colleague == cpu) {
            handleCPU((ColleagueCPU) colleague);
        }
    }

    private void handleCPU(ColleagueCPU colleague) {
        soundCard.soundPlay(colleague.getDataSound());
        graphics.videoPlay(colleague.getDataVideo());
    }

    private void handleCD(ColleagueCDDevice cdDevice) {
        cpu.decodeData(cdDevice.read());
    }

    //--------------------------------------
    public void setCdDevice(ColleagueCDDevice cdDevice) {
        this.cdDevice = cdDevice;
    }

    public void setCpu(ColleagueCPU cpu) {
        this.cpu = cpu;
    }

    public void setGraphics(ColleagueGraphics graphics) {
        this.graphics = graphics;
    }

    public void setSoundCard(ColleagueSoundCard soundCard) {
        this.soundCard = soundCard;
    }
}
